from flask import Blueprint, request, session

from app.models.tablemodel import Favorite

fa_bp = Blueprint('favorite', __name__)


@fa_bp.route('/favorite', methods=['POST'])
def add_favorite():
    data = request.get_json()
    articleid = data.get('article_id')
    action = data.get('action')  # favorite or unfavorite
    if session.get('islogin') is None:
        return 'not-login'
    elif action == 'favorite':  # 收藏
        try:
            Favorite().insert_favorite(articleid)
            return 'favorite-pass'
        except Exception:
            return 'favorite-fail'
    elif action == 'unfavorite':
        try:
            Favorite().cancel_favorite(articleid)
            return 'cancel-favorite-pass'
        except Exception:
            return 'cancel-favorite-fail'
    else:
        return 'all-fail'

# # 取消文章收藏
# @fa_bp.route('/favorite/<int:articleid>', methods=['DELETE'])
# def cancel_favorite(articleid):
#     try:
#         Favorite().cancel_favorite(articleid)
#         return 'cancel-pass'
#     except Exception:
#         return 'cancel-fail'
